﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TfsBugger.Buggers;
using TfsBugger.Controls;
using TfsBugger.Core;
using TfsBugger.Dialogs;
using TfsBugger.Forms;

namespace TfsBugger
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class AppShell : Window
    {
        public AppShell()
        {
            InitializeComponent();
            App.Shell = this;

            //Wire up common event handlers
            CommonEvents.BuggerAdded += OnBuggerAdded;
        }


        private void OnConnectButtonClick(object sender, RoutedEventArgs e)
        {
            ServerConnectDialog connectDlg = new ServerConnectDialog();
            connectDlg.ShowDialog();
        }


        private void OnCreateCheckInBuggerCommand(object sender, RoutedEventArgs e)
        {
            App.RegisterBugger(new CheckInBugger());
        }


        private void OnBuggerStackItemDoubleClick(object sender, MouseButtonEventArgs e)
        {
            BuggerStackItemControl ctl = (BuggerStackItemControl)sender;
            TabItem tab = new TabItem();
            BuggerTab ui = new BuggerTab(ctl.Bugger);
            tab.Content = ui;

            this.tabsBuggers.Items.Add(tab);
            this.tabsBuggers.SelectedItem = tab;
        }


        private void OnBuggerAdded(object sender, EventArgs e)
        {
            //this.dpBuggers.Children.Clear();
            //foreach (Bugger b in App.Buggers)
            //{
            //    BuggerStackItemControl itemCtl = new BuggerStackItemControl(b);
            //    DockPanel.SetDock(itemCtl, Dock.Top);

            //    itemCtl.MouseDoubleClick += OnBuggerStackItemDoubleClick;
            //    this.dpBuggers.Children.Add(itemCtl);
            //}
            foreach (Bugger b in App.Buggers)
            {
                bool inStack = false;
                foreach (UIElement elem in this.dpBuggers.Children)
                {
                    BuggerStackItemControl ctrl = (BuggerStackItemControl)elem;
                    if (ctrl.Bugger.Equals(b))
                    {
                        inStack = true;
                        break;
                    }
                }

                if (!inStack)
                {
                    BuggerStackItemControl itemCtl = new BuggerStackItemControl(b);
                    DockPanel.SetDock(itemCtl, Dock.Top);

                    itemCtl.MouseDoubleClick += OnBuggerStackItemDoubleClick;
                    this.dpBuggers.Children.Add(itemCtl);
                }
            }
            
        }
    }
}
